using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using TinyERP.Framework.Validation;

using TinyERP.Core.Business;
using TinyERP.Core.DTO;

using log4net;
namespace TinyERP.Core.View
{
    public class UserGroupEditInfoForm : BasicEditInfoDialog
    {
		private UserGroupDTO userGroup = new UserGroupDTO();
		private KLabel lblName;
		private TextBox txtName;
		private KLabel lblDescription;
		private TextBox txtDescription;
        private readonly ILog logger = LogManager.GetLogger(typeof(UserGroupEditInfoForm));
		public UserGroupDTO UserGroup
		{
			get 
			{
				return userGroup;
			}
			set 
			{
				userGroup = value;
				LoadEntityInfo();
			}
		}
		public IUserGroupService UserGroupService 
		{
			get
			{
				return (IUserGroupService) ServiceLocator.GetService("UserGroupServiceProxy");
			}
		}
		
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblName = new TinyERP.Framework.Controls.KLabel();
            this.txtName = new System.Windows.Forms.TextBox();
            this.lblDescription = new TinyERP.Framework.Controls.KLabel();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.contentPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonPanel
            // 
            this.buttonPanel.Location = new System.Drawing.Point(0, 98);
            // 
            // contentPanel
            // 
            this.contentPanel.Controls.Add(this.lblName);
            this.contentPanel.Controls.Add(this.txtName);
            this.contentPanel.Controls.Add(this.lblDescription);
            this.contentPanel.Controls.Add(this.txtDescription);
            this.contentPanel.Size = new System.Drawing.Size(502, 98);
            // 
            // lblName
            // 
            this.lblName.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblName.Location = new System.Drawing.Point(16, 16);
            this.lblName.Name = "lblName";
            this.lblName.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblName.ResourceKey = "";
            this.lblName.ResourceModuleKey = "Name, TinyERP.Core";
            this.lblName.Size = new System.Drawing.Size(43, 20);
            this.lblName.TabIndex = 0;
            this.lblName.Text = "Name";
            this.lblName.Values.ExtraText = "";
            this.lblName.Values.Image = null;
            this.lblName.Values.Text = "Name";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(126, 16);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(359, 20);
            this.txtName.TabIndex = 1;
            // 
            // lblDescription
            // 
            this.lblDescription.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblDescription.Location = new System.Drawing.Point(16, 41);
            this.lblDescription.Name = "lblDescription";
            this.lblDescription.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblDescription.ResourceKey = "";
            this.lblDescription.ResourceModuleKey = "Description, TinyERP.Core";
            this.lblDescription.Size = new System.Drawing.Size(73, 20);
            this.lblDescription.TabIndex = 0;
            this.lblDescription.Text = "Description";
            this.lblDescription.Values.ExtraText = "";
            this.lblDescription.Values.Image = null;
            this.lblDescription.Values.Text = "Description";
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(126, 41);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(359, 51);
            this.txtDescription.TabIndex = 2;
            // 
            // UserGroupEditInfoForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "UserGroupEditInfoForm";
            this.Size = new System.Drawing.Size(502, 128);
            this.contentPanel.ResumeLayout(false);
            this.contentPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion


		public UserGroupEditInfoForm() : base()
		{
			InitializeComponent();
			LoadEntityInfo();
		}
		
		protected override void AddValidationRules() 
		{
			//TODO: use validator to add validation rules on your input controls
			//Example: validator.AddValidationRule(new RequiredFieldRule(txtUserName, "User name"));
            validator.AddValidationRule(new RequiredFieldRule(txtName, ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("GroupName, TinyERP.Core")));
		}
		
		
		
		private void PrepareData() 
		{
		}
		
		protected override void LoadEntityInfo() 
		{
			PrepareData();
		    if(userGroup==null) 
		    {
				userGroup = new UserGroupDTO();
		    }
			txtName.Text = userGroup.Name;
			txtDescription.Text = userGroup.Description;
			deleteButton.Visible = (userGroup.ID != 0) ;
		}
        private bool IsEditMode()
        {
            return (userGroup.ID != 0);
        }
		
		protected override bool ValidateInputData() 
		{
            if (!base.ValidateInputData()) 
			{
				return false;
			}
			//TODO: add your business validation here
			return true;
		}
		
		private bool UpdateEntityInfoFromInput() 
		{
			if(!ValidateInputData()) 
			{
				return false;
			}
			userGroup.Name = txtName.Text;
			userGroup.Description = txtDescription.Text;
			return true;
		}
        
        public override void OnSave()
        {
			if(!UpdateEntityInfoFromInput()) 
			{
				return;
			}
			try {
				if(userGroup.ID==0) 
				{
					UserGroupService.SaveUserGroup(userGroup);
				}
				else 
				{
					UserGroupService.UpdateUserGroup(userGroup);
				}
				MessageBox.Show(CommonMessage.GetSaveSuccessMessage(GetEntityResourceKey()));
                InvokeOnNeedReloadData();
                InvokeCloseDialog();
			} 
			catch(Exception ex) 
			{
                logger.Error(ex); 
				MessageBox.Show(CommonMessage.GetCanNotSaveError(GetEntityResourceKey()));
			}
        }

        public override void OnDelete()
        {
			try 
			{
                if (ConfirmDelete() == DialogResult.OK)
                {
					UserGroupService.DeleteUserGroupByID(userGroup.ID);			
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage(GetEntityResourceKey()));
                    InvokeOnNeedReloadData();
                    InvokeCloseDialog();
				}
			}
			catch(Exception ex) 
			{
                logger.Error(ex);			
				MessageBox.Show(CommonMessage.GetCanNotDeleteError(GetEntityResourceKey()));
			}
        }

        
        public override string GetHeaderText()
        {
            return ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("UserGroupEditForm, TinyERP.Core");
        }

        private string GetEntityResourceKey()
        {
            return ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("UserGroup, TinyERP.Core");
        }

        private DialogResult ConfirmDelete()
        {
            return MessageBox.Show(CommonMessage.GetDeleteConfirmMessage(GetEntityResourceKey()),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel);
        }

	}
}

